package com.qs.education.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationTeacherGradeClassResponse;
import com.qs.education.bean.beans.EducationTeacherResponse;
import com.qs.education.bean.entity.EducationTeacher;
import com.qs.education.service.IGradeAPPService;
import com.qs.education.service.ITeacherAPPService;

/**
 * EducationTeacherController
 * 
 * @author Leon
 * @date Nov 1, 2018
 * @return void
 */
@RestController
@RequestMapping("/teacher")
public class EducationTeacherController {

	@Autowired
	private ITeacherAPPService iTeacherAPPService;

	@Autowired
	private IGradeAPPService iGradeAPPService;

	private final Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description 教师端 根据手机号查询教师用户是否存在
	 * @author Leon
	 * @date Nov 5, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/findTeacherByMobile")
	public Map<String, Object> findTeacherByMobile(String mobile) {
		System.out.println("app controller:" + mobile);
		log.info("APP Teacher 根据手机号查询 教师用户是否存在");
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.findTeacherByMobile(mobile);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 根据教师 手机号或者姓名进行查询
	 * @author Leon
	 * @date Nov 12, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/findTeacherByMobileOrName")
	public Map<String, Object> findTeacherByMobileOrName(String content) {
		System.out.println("app controller ");
		System.out.println("content:" + content);
		ResponseListBean<EducationTeacher> list = new ResponseListBean<EducationTeacher>();
		list = iTeacherAPPService.findTeacherByMobileOrName(content);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", list);
		return map;
	}

	/**
	 * @Description 教师端登录操作
	 * @author Leon
	 * @date Nov 6, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/login")
	public Map<String, Object> login(String mobile, String password) {
		System.out.println("app controller  mobile,password:" + mobile + " " + password);
		log.info("APP Teacher 根据手机号查询 教师用户是否存在");
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.login(mobile, password);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 教师端退出操作
	 * @author Leon
	 * @date Nov 6, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/quit")
	public Map<String, Object> Quit(String mobile) {
		System.out.println("app controller mobile:" + mobile);
		log.info("App Teacher 退出登录");
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.Quit(mobile);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 教师端忘记密码
	 * @author Leon
	 * @date Nov 6, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/forgetPassword")
	public Map<String, Object> ForgetPassword(String mobile, String password) {
		System.out.println("app controller forgetPassword mobile,code,password" + mobile + " " + password);
		log.info("App Teacher 忘记密码");
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.ForgetPassword(mobile, password);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 注册成功保存手机号和密码
	 * @author Leon
	 * @date Nov 7, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/saveTeacherMobileAndPassword")
	public Map<String, Object> saveTeacherMobileAndPassword(String mobile, String password) {
		System.out.println("app controller saveTeacherMobileAndPassword");
		log.info("app controller 注册成功保存手机号和密码");
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.saveTeacherMobileAndPassword(mobile,
				password);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 完善教师信息 姓名 学校 科目
	 * @author Leon
	 * @date Nov 8, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/perfectTeacherInfo")
	public Map<String, Object> perfectTeacherInfo(EducationTeacher teacher, String schoolId, String subjectId) {
		// 1. 完善teacher 的姓名 学校 科目
		EducationTeacherResponse educationTeacherResponse = iTeacherAPPService.perfectTeacherInfo(teacher, schoolId,
				subjectId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("educationTeacherResponse", educationTeacherResponse);
		return map;
	}

	/**
	 * @Description 完善教师信息 年级 和 班级
	 * @author Leon
	 * @date Nov 9, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/perfectTeacherGradeAndClass")
	public Map<String, Object> perfectTeacherGradeAndClass(@RequestBody String params) {
		System.out.println("apps controller perfectTeacherGradeAndClass");
		System.out.println(params);
		EducationTeacherGradeClassResponse response = iGradeAPPService.batchSaveGradeAndClass(params);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("response", response);
		return map;
	}

	/**
	 * @Description 修改教师个人信息
	 * @author Leon
	 * @date Nov 15, 2018
	 * @param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/modifyTeacherInfo")
	public Map<String, Object> modifyTeacherInfo(HttpServletRequest request, EducationTeacher teacher,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		log.info("apps teacher modify");
		Map<String, Object> map = new HashMap<String, Object>();
		EducationTeacherResponse response = iTeacherAPPService.modifyTeacherInfo(request, teacher, file);
		map.put("educationTeacherResponse", response);
		return map;
	}

	/**
	 * @Description 退出群组
	 * @author Leon
	 * @date   Nov 19, 2018
	 * @param  
	 * @return Map<String,Object>
	 */
	@RequestMapping("/quitGroup")
	public Map<String, Object> quitGroup(String groupId, String teacherId) {
		log.info("apps teacher quitGroup");
		ResponseBean response = new ResponseBean();
		response = iTeacherAPPService.quitGroup(groupId, teacherId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("response", response);
		return map;
	}
	
	/**
	 * @Description 修改密码
	 * @author Leon
	 * @date   Nov 27, 2018
	 * @param  
	 * @return ResponseBean
	 */
	@RequestMapping("/changePassword")
	public ResponseBean changePassword(String id,String oldPassword,String newPassword) {
		ResponseBean response = new ResponseBean();
		response = iTeacherAPPService.changePassword(id,oldPassword, newPassword);
		return response;
	}
	
	/**
	 * @Description 根据教师id查询教师信息 
	 * @author Leon
	 * @date   Nov 28, 2018
	 * @param  
	 * @return EducationTeacherResponse
	 */
	@RequestMapping("/findTeacherById")
	public EducationTeacherResponse findTeacherById(String teacherId) {
		EducationTeacherResponse response = new EducationTeacherResponse();
		response = iTeacherAPPService.findTeacherById(teacherId);
		return response;
	}
}